$(function () {
  let laypage = layui.laypage
  let layer = layui.layer
  let q = {
    page: 1,  //当前请求的页码，默认为第1页
    limit: 10,  //每页要显示的数据条数，默认为10条
    cateid: localStorage.getItem('onlyListId')  //对应的分类ID，可以根据该ID获取对应的新闻列表
  }
  ListRender()
  function ListRender() {
    //发送get请求
    axios.get('/index/list', { params: q }).then(function (response) {
      const res = response.data
      console.log(res, 'ppppppppppppppp');
      if (res.errno !== 0) {
        return layer.msg(res.errmsg)
      }
      $('.cat-leader h1').html(res.data.list.data[0].catename)
      console.log(res.data.list.data[0].catename);
      const htmlStr = res.data.list.data.map(item => `
              <article class="excerpt excerpt-one" data-id="${item.id}">
                <header>
                  <a class="cat label label-important clickA" href="">${item.ishot ? '热门' : '推荐'}<i class="label-arrow"></i></a>
                  <h2>
                    <a target="_blank" href="show.html?id=${item.id}" title="${item.title}" class="clickA">${item.title}</a>
                  </h2>
                </header>
                <p class="text-muted time">
                  <a href="">${item.author}</a> 发布于 ${new Date(item.create_date).toLocaleString()}
                </p>
                <p class="focus">
                  <a target="_blank" href="show.html" class="thumbnail clickA">
                    <span class="item"><span class="thumb-span"><img src="http://124.223.14.236:8060/${item.pic}"
                          class="thumb" /></span></span></a>
                </p>
                <p class="note">${item.description}</p>
                <p class="text-muted views">
                  <span class="post-views">阅读(${item.click})</span>
                  <span class="post-comments">评论(0)</span>
                  <a href="javascript:;" class="post-like">
                    <i class="fa fa-thumbs-up"></i>赞 (<span>${item.lovenum}</span>)
                  </a>
                  <span class="post-tags">标签：
                    <a href="" rel="tag">${item.tags}</a>
                </p>
              </article>
          `).join('')
      $('.articleList').html(htmlStr)
      renderPage(res.data.list.count)
    })
      .catch(function (error) {
        console.error(error)
      })
  }
  // 分页器
  function renderPage(total) {
    //执行一个laypage实例
    laypage.render({
      elem: 'page-box',
      count: total, //数据总数，从服务端得到
      limit: q.limit, // 设置每页多少条数据
      curr: q.page, // 设置当前页码位置，防止重新渲染的时候页码回到1
      layout: ['prev', 'page', 'next', 'last', 'count', 'groups', 'skip'],
      jump: function (obj, first) {
        q.page = obj.curr //点击跳转页面，调用了jump
        if (!first) {
          // 请求列表数据
          ListRender()
        }
      }
    })
  }
})

//热门标签
const widget_tags = document.querySelector('.widget_tags')
axios.get('/index/index')
  .then(res => {
    console.log(res)
    const widget_tagsData = res.data.data.allTag.map(item => {
      return `
      <ul class="widget_tags_inner" style="display: inline-block;width: 120px;height:110">
      <li style="display: inline-block;width: 120px;"><a title="" href="">${item.tagname}</a></li>
        </ul>  `
    }).join('')
    $('.widget_tags').append(widget_tagsData)
  })

  //友情链接
const widget_comments = document.querySelector('.widget_comments')
axios.get('/index/index')
  .then(res => {
    // console.log(res)
    const widget_commentsData = res.data.data.allLink.map(item => {
      return `
      <li style="width: 110px;display: inline-block;">
      <a href=""><img src="http://124.223.14.236:8060/${item.img}" alt="">
        <p>${item.title}</p>
      </a>
      </li>`
    }).join('')
    $('.widget_comments').html(widget_commentsData)
  })


  //底部链接
  const link = document.querySelector('.link')
  axios.get('/index/index')
  .then(res => {
    console.log(res)
    const linkData = res.data.data.allLink.map(item=>{
      return`<a href="${item.url}" title="${item.des}">${item.des}</a> <span>|</span>`
    }).join('')
    $('.link').html(linkData)
  })


function capture(data) {
  localStorage.setItem('onlyListId', data)
}
